<template>
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="500px"
    destroy-on-close
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <!-- 只读状态显示遮罩 -->
      <div v-if="props.isReadonly" class="formCover"></div>

      <el-form-item label="角色名称" prop="roleName">
        <el-input v-model="formData.roleName" placeholder="请输入角色名称" />
      </el-form-item>

      <el-form-item label="角色备注" prop="remarks">
        <el-input
          v-model="formData.remarks"
          placeholder="请输入角色备注"
          type="textarea"
          show-word-limit
          :maxlength="250"
          :autosize="{ minRows: 3, maxRows: 4 }"
        />
      </el-form-item>

      <el-form-item label="状态" prop="enabled">
        <el-radio-group v-model="formData.enabled">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="0">停用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>

    <template v-if="!props.isReadonly" #footer>
      <div class="dialog-footer">
        <el-button type="primary" :loading="loading" @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";

defineOptions({
  name: "AddRole",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

/** 弹窗显示隐藏 */
const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

/** 定义form信息 */
const rules = reactive({
  roleName: [
    { required: true, message: "请输入角色名称", trigger: "blur" },
    { max: 45, message: "角色名称不能超过45个字符", trigger: "blur" },
  ],
  remarks: [{ message: "请输入角色备注", trigger: "blur" }],
  enabled: [{ required: true, message: "请选择状态", trigger: "blur" }],
});

const formRef = ref(ElForm);

const initForm = {
  enabled: 1,
  remarks: "",
  roleName: "",
  id: "",
};
let formData = reactive(JSON.parse(JSON.stringify(initForm)));

/** 编辑赋值 */
watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const { id, roleName, remarks, enabled } = newVal;
      Object.assign(formData, { id, roleName, remarks, enabled });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
}

const loading = ref(false);

/** 角色保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/system/sysRole/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(`${text}成功`);
      emit("reset-table", formData.id || "");
      closeDialog();
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
<style lang="scss" scoped>
.el-form {
  position: relative;
  .formCover {
    position: absolute;
    top: 0;
    width: 100%;
    left: 0;
    bottom: 0;
    right: 0;
    z-index: 2000;
    cursor: default;
  }
}
</style>
